<script lang="ts" setup>
import { computed, watch } from 'vue';
import { AuthenticationLoginExpiredModal } from '@vben/common-ui';
import { useWatermark } from '@vben/hooks';

import {
  BasicLayout,
  LockScreen,
  UserDropdown,
} from '@vben/layouts';
import { preferences } from '@vben/preferences';
import { useAccessStore, useUserStore } from '@vben/stores';
import { useAuthStore } from '#/store';
import LoginForm from '#/views/_core/authentication/login.vue';
import { BookOpenText } from '@vben/icons';
// import EditExtraModal from '#/views/manage/user/edit-modal.vue';
import EditExtraModal from './edit-modal.vue';
import {useVbenModal} from '@vben/common-ui';
import {getUserInfo} from '#/api/gis/user';
import { useRefresh } from '@vben/hooks';

const { refresh } = useRefresh();


const userStore = useUserStore();
const authStore = useAuthStore();
const accessStore = useAccessStore();
const { destroyWatermark, updateWatermark } = useWatermark();

const [EditModal, EditmodalApi] = useVbenModal({
  connectedComponent: EditExtraModal,
  footer:false,
});


// 定义菜单项
const menus = computed(() => [{
  handler: async () => {
    const userProfile = await getUserInfo(); // 调用接口获取用户信息
    console.log('用户信息：', userProfile);
    if (userProfile) {
      openEditModal(userProfile); // 确保 data 存在
    } else {
      console.error('用户信息获取失败或为空');
    }
  },
  icon: BookOpenText,
  text: '编辑资料',
}]);

 // 打开修改模态框
function openEditModal(row: any) {

  const row1: any = {
    userId: row.userId,
    status: parseInt(row.status || '0'), // 默认值为 '0' 避免空值
    sex: parseInt(row.sex || '0'), // 默认值为 '0' 避免空值
    realName: row.nickName || '', // 默认值为 ''
    phoneNumber: row.phoneNumber || '', // 默认值为 ''
    email: row.email || '', // 默认值为 ''
    remark: row.remark || '', // 默认值为 ''
    avatar: row.avatar || '',
    userName: row.userName || '', // 添加 userName 字段
  };

  EditmodalApi
    .setData({
      data: row1,
    });
    
  console.log("编辑传递的行数据为：", row1);
  EditmodalApi.open();
}


const avatar = computed(() => {
  return userStore.userInfo?.avatar ?? preferences.app.defaultAvatar;
});

async function handleLogout() {
  await authStore.logout(false);
}

watch(
  () => preferences.app.watermark,
  async (enable) => {
    if (enable) {
      await updateWatermark({
        content: `${userStore.userInfo?.username}`,
      });
    } else {
      destroyWatermark();
    }
  },
  {
    immediate: true,
  },
);
</script>

<template>
  <EditModal @refreshTable="refresh"/>
  <BasicLayout @clear-preferences-and-logout="handleLogout">
    <template #user-dropdown>
      <UserDropdown
        :avatar
        :menus
        :text="userStore.userInfo?.userName"
        :description="userStore.userInfo?.realName"
        tag-text="采油站"
        @logout="handleLogout"
      />
    </template>

    <template #extra>
      <AuthenticationLoginExpiredModal
        v-model:open="accessStore.loginExpired"
        :avatar
      >
        <LoginForm />
      </AuthenticationLoginExpiredModal>
    </template>

    <template #lock-screen>
      <LockScreen :avatar @to-login="handleLogout" />
    </template>
    
  </BasicLayout>
</template>
